/* 
 * File:   teststringutils.cpp
 * Author: mariano
 *
 * Created on December 24, 2009, 7:23 PM
 */

#include <stdlib.h>
#include <iostream>
#include "utils/stringutils.h"
#include <cassert>
#include <string>
using namespace std;

void testCleaning(){
    string s = "-00000123";
    StringUtils::limpiarEntero(s);
    assert(s == "123");
    s = "9990000";
    StringUtils::limpiarEntero(s);
    assert(s == "9990000");
    s = "-123";
    StringUtils::limpiarEntero(s);
    assert(s == "123");
    s = "0000123";
    StringUtils::limpiarEntero(s);
    assert(s == "123");
    s = "";
    StringUtils::limpiarEntero(s);
    assert(s == "");
    s = "0000123";
    StringUtils::limpiarFraccion(s);
    assert(s == "0000123");
    s = "1230000";
    StringUtils::limpiarFraccion(s);
    assert(s == "123");
    cout << "OK : Cleaning" << endl;
}

void testSignoYSeparar() {
    string s, ent, fra;
    s = "0000123";
    ent = "";
    fra = "";
    assert(!StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "");
    
    s = "-00000123";
    ent = "";
    fra = "";
    assert(StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "");

    s = "-123";
    ent = "";
    fra = "";
    assert(StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "");

    s = "0000123,2";
    ent = "";
    fra = "";
    assert(!StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "2");

    s = "-0000123,2";
    ent = "";
    fra = "";
    assert(StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "2");

    s = "abcde";
    ent = "";
    fra = "";
    assert(!StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "" && fra == "");

    s = "-0000123,2000000";
    ent = "";
    fra = "";
    assert(StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "2");

    s = "-0000123,000000000002000000";
    ent = "";
    fra = "";
    assert(StringUtils::signoYSeparar(s, ent, fra));
    assert(ent == "123" && fra == "000000000002");

    cout << "OK : Signo y separar" << endl;
}

void testNocero(){
    string s;
    s = "";
    assert(!StringUtils::noCero(s));
    s = "0001";
    assert(StringUtils::noCero(s));
    s = "100000";
    assert(StringUtils::noCero(s));
    s = "asd";
    assert(StringUtils::noCero(s));

    cout << "OK : noCero" << endl;
}

void test2bin() {
    string s;
    s = "1";
    StringUtils::entChar2bin(s);
    assert(s == "00000001");

    s = "1236698845";
    StringUtils::entChar2bin(s);
    assert(s == "01001001101101101000011011011101");

    s = "2";
    StringUtils::entChar2bin(s);
    assert(s == "00000010");

    s = "5";
    StringUtils::fracChar2bin(s, 15);
    assert(s == "10000000");
    
    s = "125";
    StringUtils::fracChar2bin(s, 15);
    assert(s == "00100000");

    s = "625";
    StringUtils::fracChar2bin(s, 15);
    assert(s == "10100000");

    s = "00125";
    StringUtils::fracChar2bin(s, 15);
    assert(s == "000000000101000111101011100001010001111010111000010100011110101110000101000111101011100001010001111010111000010100011110");

    cout << "OK : decimal a binario" << endl;
}

void testCalculoPotencia() {
    string ent, fra;
    int potencia,expected;
    ent = "1";
    fra = "011100";
    potencia = StringUtils::calcularPotencia(ent, fra);
    expected = 1;
    assert(expected == potencia);

    ent = string("100")+"01001011"+"01010101"+"00010101";
    fra = "011100";
    potencia = StringUtils::calcularPotencia(ent, fra);
    expected = 4;
    assert(expected == potencia);

    // 0,00X
    ent = "";
    fra = string("00000000")+"00000000"+"00100000";
    potencia = StringUtils::calcularPotencia(ent, fra);
    expected = -2;
    assert(expected == potencia);
    assert(fra == "00100000");

    ent="";
    fra="";
    potencia = StringUtils::calcularPotencia(ent, fra);
    expected = 0;
    assert(expected == potencia);
    assert(fra == "");
    cout << "OK : Calculo de potencia" << endl;

}

/*
 * 
 */
void teststringutils() {
    cout << endl << "STRING UTILS TEST" << endl;
    testCleaning();
    testSignoYSeparar();
    testNocero();
    test2bin();
    testCalculoPotencia();
}

